package com.arvin.service.serviceImpl;

import com.arvin.mapper.ExamMapper;
import com.arvin.mapper.StudentMapper;
import com.arvin.pojo.Contest;
import com.arvin.pojo.Exam;
import com.arvin.pojo.ExamInfo;
import com.arvin.pojo.PageBean;
import com.arvin.service.ExamService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * @author tengyue
 * @date 2023/12/6/10:25
 */
@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 获取学生笔试信息
     * @param page
     * @param pageSize
     * @param studentId
     * @param stuName
     * @param className
     * @param examDate
     * @param examState
     * @param examScore
     * @return
     */
    @Transactional
    public PageBean getExamInfo(Integer page, Integer pageSize, String studentId,
                                String stuName,String className, LocalDate examDate,
                                String examState,Integer examScore) {
        PageHelper.startPage(page,pageSize);
        List<ExamInfo> examInfoList = examMapper.getExamInfo(studentId,stuName,className,examDate,examState,examScore);
        for (ExamInfo examInfo: examInfoList){
            List<Contest> list = studentMapper.findByStudentIdToContest(examInfo.getStudentId());
            examInfo.setContest(list);
        }
        Page<ExamInfo> examInfoPage = (Page<ExamInfo>) examInfoList;
        PageBean pageBean = new PageBean(examInfoPage.getTotal(),examInfoPage.getResult());
        return pageBean;
    }

    /**
     * 更新学生笔试状态
     * @param examId
     * @param examState
     */
    public void updateExamState(Integer examId, String examState,Integer examScore) {
        Exam exam = new Exam();
        exam.setExamId(examId);
        exam.setExamState(examState);
        exam.setExamScore(examScore);
        examMapper.updateExamInfo(exam);
    }

    /**
     * 学生批量通过或不通过考试
     * @param ids
     */
    @Override
    public void passByExamIds(List<Integer> ids,String examState) {
        examMapper.updateStateByExamIds(ids,examState);
    }
}
